package de.traveltainment.concept.envers.repository;

import de.traveltainment.concept.envers.audit.lifecycle.Lifecycle;
import de.traveltainment.concept.envers.audit.lifecycle.Stage;
import de.traveltainment.concept.envers.database.Constants;
import de.traveltainment.concept.envers.entities.ChangeType;
import de.traveltainment.concept.envers.entities.Provider;
import de.traveltainment.concept.envers.entities.Vito;
import de.traveltainment.concept.envers.entities.VitoXProvider;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by borlis on 13.06.2014.
 */
@RequestScoped
public class RelationRepository {

    @Inject
    private EntityManager em;

    public VitoXProvider create(VitoXProvider t) {
        em.getTransaction().begin();
        t.setChangedBy(Constants.SYSTEM_USER);
        t.setChangedOn(Calendar.getInstance().getTime());
        t.setChangedType(ChangeType.I);
        t.setLocked(Constants.UNLOCKED);
        em.persist(t);
        em.getTransaction().commit();
        return t;
    }

    public VitoXProvider update(VitoXProvider t) {
        em.getTransaction().begin();
        t.setChangedBy(Constants.SYSTEM_USER);
        t.setChangedOn(Calendar.getInstance().getTime());
        t.setChangedType(ChangeType.I);
        t.setLocked(Constants.UNLOCKED);
        em.merge(t);
        em.getTransaction().commit();
        return t;
    }

    public void linkVitoToAllProviders(Vito vito) {
        em.getTransaction().begin();
        Query query = em.createNamedQuery("linkVitoToAllproviders").setParameter("xCode", vito.getId());
        query.executeUpdate();
        em.getTransaction().commit();
    }

    public void linkVitoToLimitedProviders(Vito vito, int i, int i1) {
        em.getTransaction().begin();
        Query query = em.createNamedQuery("linkVitoToLimitedProviders").setParameter("xCode", vito.getId());
        query.executeUpdate();
        em.getTransaction().commit();
    }

    private List<Provider> findProviders(String ctr) {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Provider> criteria = builder.createQuery(Provider.class);
        Root<Provider> from = criteria.from(Provider.class);
        criteria.where(builder.equal(from.get("country"), ctr));
        TypedQuery<Provider> query = em.createQuery(criteria);
        return query.getResultList();
    }

    private List<VitoXProvider> createList(Vito vito, List<Provider> providers) {
        List<VitoXProvider> vitoXProviderList = new ArrayList<VitoXProvider>(providers.size());
        for (Provider provider : providers) {
            VitoXProvider vitoXProvider = new VitoXProvider();
            vitoXProvider.setVito(vito);
            vitoXProvider.setProvider(provider);
            vitoXProviderList.add(vitoXProvider);
        }
        return vitoXProviderList;
    }

    public void link(Vito vito, String country) {
        try {
            List<VitoXProvider> vitoXProviderList = createList(vito, findProviders(country));
            em.getTransaction().begin();
            for (VitoXProvider vitoXProvider : vitoXProviderList) {
                em.persist(vitoXProvider);
            }
            em.getTransaction().commit();
        } catch (Exception error) {
            error.printStackTrace();
        }

    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public void linkVitoToAllProviders(Vito vito, int quontaty) {

    }
}
